<template>
  <el-dialog
    ref="refDlg"
    title="查看评估详情"
    :visible.sync="dialogShow"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :before-close="clearForm"
    width="800px"
    @close="closeDialog"
  >
    <div>
      <div v-if="flag === 'unitProEvaluation'">
        <el-form ref="refForm" class="financeForm" :model="queryObj" size="mini" label-width="120px">
          <el-row>
            <el-col :span="12">
              <el-form-item label="门店建设项目号:" prop="shopCode">
                <span>{{ queryObj.constCode }}</span>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="所属门店:" prop="customerCode">
                <span>{{ queryObj.shopName }}</span>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="装修任务号:" size="mini">
                <span>{{ queryObj.decorationTaskCode }}</span>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <el-table
        v-loading="tableLoading"
        element-loading-text="加载中"
        :data="tableData"
        :span-method="objectSpanMethod"
        border
        highlight-current-row
        :header-cell-style="{background:'#F6F6F6', borderRight:'1px solid #EBEEF5'}"
        style="width: 100%; margin-top: 20px"
      >
        <el-table-column prop="scoreNature" label="评估性质" min-width="100">
          <template slot-scope="scope">
            <span>{{ formaterData(scoreNatureList, scope.row.scoreNature) }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="scoreType" label="评估类别" min-width="100" />
        <el-table-column prop="totalScore" label="总分值" />
        <el-table-column prop="supervisorScore" label="工程监理评分" show-overflow-tooltip min-width="100" />
        <el-table-column prop="customerScore" label="经销商评分" min-width="100" />
        <el-table-column prop="acceptGroupScore" label="验收项目组评分" min-width="120" />
        <el-table-column prop="actualScore" label="实际得分" />
        <el-table-column prop="detailItem" label="考核明细" min-width="150" />
        <el-table-column prop="grading" label="评分标准" show-overflow-tooltip min-width="150" />
        <el-table-column prop="scoreDesc" label="评分说明" min-width="100" />
      </el-table>
    </div>
    <div slot="footer">
      <el-button size="mini" @click="closeDialog">返回</el-button>
      <!-- <el-button size="mini" type="primary" @click="submitForm">确定</el-button> -->
    </div>
  </el-dialog>
</template>

<script>
import { queryProEvaluation } from '@/api/constructionUnitMng'
import { getDictByPatchType } from '@/api/dataDict'
export default {
  name: 'ProEvaluationDlg',
  props: {
    visible: {
      type: Boolean,
      default: false,
      required: true
    },
    detailData: {
      type: [String, Object],
      default: ''
    }
  },
  data() {
    return {
      tableLoading: false,
      flag: '',
      tableData: [],
      rowTotal: [],
      scoreNatureList: [],
      queryObj: {
        id: ''
      }
    }
  },

  computed: {
    dialogShow: {
      get() {
        return this.visible
      },
      set(val) {
        this.$emit('update:visible', val)
      }
    }
  },
  async created() {
    await this.getDictByPatchTypeAll(['SCORE_NATURE']).then(res => {
      this.scoreNatureList = res.SCORE_NATURE
    })
    this.tableLoading = true
    this.queryObj = this.detailData.data
    this.flag = this.detailData.flag
    const obj = { 'scoreNature': this.queryObj.scoreNature }
    const filtersRaw = [{ 'id': 'decorationTaskCode', 'property': 'decorationTaskCode', 'operator': '=', 'value': this.queryObj.decorationTaskCode },
      { 'id': 'unitCode', 'property': 'unitCode', 'operator': '=', 'value': this.queryObj.unitCode }]
    const formData = new FormData()
    // formData.append('decorationTaskCode', this.queryObj.decorationTaskCode)
    // formData.append('unitCode', this.queryObj.unitCode)
    formData.append('paramsRaw', JSON.stringify(obj))
    formData.append('filtersRaw', JSON.stringify(filtersRaw))
    queryProEvaluation(formData).then(res => {
      if (res.success && res.data && res.data.length) {
        const arr = []
        res.data.forEach(row => {
          if (row.assessDetailsDtos && row.assessDetailsDtos.length) {
            row.assessDetailsDtos.forEach((item, index) => {
              item.scoreNature = row.scoreNature
              item.scoreType = row.scoreType
              item.totalScore = row.totalScore
              item.supervisorScore = row.supervisorScore
              item.customerScore = row.customerScore
              item.acceptGroupScore = row.acceptGroupScore
              item.actualScore = row.actualScore
              if (index === 0) {
                item.spanIndex = row.assessDetailsDtos.length
              }
              arr.push(item)
            })
          }
        })
        this.tableData = this.handlerData(arr)
      }
      this.tableLoading = false
    })
  },

  methods: {
    // 获取数据字典
    async getDictByPatchTypeAll(data) {
      const obj = await getDictByPatchType(data)
        .then(res => {
          return res
        })
      return obj
    },
    // 格式化数据
    formaterData(arr, dictCode) {
      const tmpArr = arr.filter(item => item.dictCode === dictCode)
      if (tmpArr.length) {
        return tmpArr[0].dictName
      }
      return dictCode
    },
    // 处理数据
    handlerData(data) {
      const t = {}
      for (const item of data) {
        const key = item.scoreNature
        if (t[key] === undefined) {
          t[key] = []
        }
        t[key].push(item)
      }
      const result = []
      for (const [k, v] of Object.entries(t)) {
        const programs = {}
        for (let i = 0; i < v.length; i++) {
          const item = v[i]
          if (i === 0) {
            item.natureSpanRow = v.length
            item.natureSpanColumn = 1
          } else {
            item.natureSpanRow = 0
            item.natureSpanColumn = 0
          }
          const key = item.scoreType
          if (programs[key] === undefined) {
            programs[key] = []
          }
          programs[key].push(item)
        }
        for (const [k, v] of Object.entries(programs)) {
          v.forEach((e, i) => {
            if (i === 0) {
              e.typeSpanRow = v.length
              e.typeSpanColumn = 1
            } else {
              e.typeSpanRow = 0
              e.typeSpanColumn = 0
            }
            result.push(e)
          })
        }
      }
      return result
    },
    /**
     * 表格合并
     * @param {*} param0
     * row 表格每一行的数据
     * column 表格每一列的数据
     * rowIndex 表格的行索引,不包括表头,从0开始
     * columnIndex 表格的列索引,从0开始
     */
    objectSpanMethod({ row, column, rowIndex, columnIndex }) {
      if (columnIndex === 0) {
        return {
          rowspan: row.natureSpanRow,
          colspan: row.natureSpanColumn
        }
      } else if (columnIndex >= 1 && columnIndex <= 6) {
        return {
          rowspan: row.typeSpanRow,
          colspan: row.typeSpanColumn
        }
      }
      return {
        rowspan: 1,
        colspan: 1
      }
    },
    // 关闭弹窗
    closeDialog() {
      this.dialogShow = false
      this.clearForm()
    },
    // 关闭弹窗前的回调
    clearForm(done) {
      this.queryObj = {
        id: null
      }
      this.$refs.refForm && this.$refs.refForm.clearValidate()
      done && done()
    }
  }
}
</script>
<style lang='scss' scoped>

</style>
